#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/12/9 0:08
# @Author  : lizhen
# @Site    : 
# @File    : iterator.py
# @Software: PyCharm
from collections import Iterable, Iterator


class MyInterableObject(Iterable):
    def __init__(self, text):
        self.text = text

    def __repr__(self):
        return 'execute MyInterableObject({})'.format(repr(self.text))

    def __iter__(self):
        return CustomInterator(self.text)


class CustomInterator(Iterator):
    def __init__(self, words):
        self.words = words
        self.index = 0

    def __next__(self):
        try:
            word = self.words[self.index]
        except IndexError:
            raise StopIteration
        self.index += 1
        return word

    def __iter__(self):
        return self


def main():
    mio = MyInterableObject('abc')
    print(mio)
    for item in mio:
        print(item)
    assert isinstance(MyInterableObject('abc'), Iterable)
    assert not isinstance(MyInterableObject('abc'), Iterator)
    assert isinstance(CustomInterator('abc'), Iterable)
    assert isinstance(CustomInterator('abc'), Iterator)


if __name__ == '__main__':
    main()
